<?php

namespace App\Helpers\Libraries;

use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\Result\ResultInterface;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\Response;
use Intervention\Image\AbstractFont;
use Intervention\Image\ImageManager;
use phpDocumentor\Reflection\Types\Collection;
use PhpParser\Node\Expr\Cast\Object_;

class ImageLibrary
{
    /**
     * 生成二维码
     * @param int $isResponse 是否直接响应图片
     * @param string $data 二维码数据内容
     * @param int $size 大小
     * @param int $margin 边距
     * @return Response|mixed|string
     * @throws BindingResolutionException
     */
    public static function createQRCode(int $isResponse, string $data, int $size = 290, int $margin = 1)
    {
        $writer = new PngWriter();
        $qrCode = QrCode::create($data) // 数据
            ->setEncoding(new Encoding('UTF-8'))
            ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
            ->setSize($size) // 尺寸
            ->setMargin($margin) // 边距
            ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
            ->setForegroundColor(new Color(0, 0, 0))
            ->setBackgroundColor(new Color(255, 255, 255));
        if (!$isResponse){
            return $writer->write($qrCode)->getString();
        }
        return \response()->make($writer->write($qrCode)->getString())
            ->header('Content-Type', $writer->write($qrCode)->getMimeType());
    }

    /**
     * 合成海报
     * @param string $data 二维码数据
     * @param string $shareImagePath 分享海报底图路径
     * @param string $text1 文本1
     * @param string $text2 文本2
     * @param string $text3 文本3
     * @return Response|mixed
     * @throws BindingResolutionException
     */
    public static function createShareImage(string $data, string $shareImagePath, string $text1, string $text2, string $text3)
    {
        $manager = new ImageManager(array('driver' => 'gd'));
        $qrCode = self::createQRCode(0, $data, 460);
        $image = $manager->make(resource_path($shareImagePath)) // 海报图
            ->insert($qrCode, 'top_left', 390, 830) // 插入二维码
            ->text($text1, 280 ,740, function (AbstractFont $font){ // 插入文字
                $font->file(resource_path('ttf/msyh.TTF'));
                $font->size(36); // 字体大小
                $font->color('#FFFFFF'); //字体颜色
                $font->align('top'); // 水平对齐
                $font->valign('top'); // 垂直对齐
                $font->angle(0); // 旋转角度
            })
            ->text($text2, 280 ,1600, function (AbstractFont $font){
                $font->file(resource_path('ttf/msyh.TTF'));
                $font->size(36);
                $font->color('#CE93D8');
            })
            ->text($text3, 280 ,1700, function (AbstractFont $font){
                $font->file(resource_path('ttf/msyh.TTF'));
                $font->size(36);
                $font->color('#CE93D8');
            });
        return \response()->make($image->encode())->header('Content-Type', 'image/png');
    }
}
